Timepix3

If you are interested in my expertise with timepix3 chips and how to efficiently operate on their data (even in almost real time!), feel free to contact me. I am always happy to help researchers with these interesting devices.

Timepix3 is a sensor chip specification for particle detectors. I have worked with both electron and photon cameras based on timepix3 chips. Timepix3 cameras can function both like a normal camera ("counting mode") and evented mode, which the focus of my work and this little article.

Normal cameras read out a complete image from all pixels periodically in a cascade-like manner. Evented read out however treats each pixel as an independent detector, generating a digital event for each change in intensity for every individual pixel. In the case of timepix3, pixels can be either on or off and the signal includes the time it was activated (Time of Arrival, ToA) and the time how long it remained activated (Time over Threshold, ToT). This timing is very precise and can be linked back to whatever caused the pixel to be activated. Timepix3 based cameras can, under the right conditions, be used to identify individual particles in both time and space.

Timepix3 Data

The format used to send timepix3 data over the network, and often also to store it on disk, is a stream of 64 bit words. A magic number identifies each word as a specific packet. These packets are grouped into chunks preceded by a special header packet.

The support electronics used in conjunction with the timepix3 chip (for example the SPIDR system) introduce their own packets into this stream that follow the same general structure.

The magic number of the header packet is 0x33585054, "TPX3" as u32, in the least significant bits. As such it doubles as the files magic identifier. The header packet also contains the sensor chip index of all packets contained in this chunk, as well as the chunk size.

const Header = packed struct(u64) {
    magic: u32, // Must be 0x33585054
    chip_index: u8,
    _reserved: u8,
    chunk_size: u16,
};

All other packets have their magic number in the most significant bytes.

// Only sent in evented mode. Counted mode has its own packet.
const PixelActivation = packed struct(u64) {
    spide_time: u16, // If used with SPIDR, counts 0.4096ms
    toa_fine: u8,    // Counts -1.5625ns
    tot: u10         // Counts 25ns
    toa_coarse: u15  // Counts 25ns
    pix: u3,
    super_pix: u6,
    col: u7,
    magic: u4, // Must be 0xb
};

This might look a bit strange; Pixels are arranged in super pixels, a block of eight pixels sharing some of the read out mechanism. The pixel activation packet contains the index of the pixel inside the super pixel as well as the super pixels location on the sensor. The pixel column and row, as well as the ToA can be derived from a pixel activation packet as follows.

const dcol = @as(u8, @intCast(pixel_activation.col)) << 1;
const super_pix = @as(u8, @intCast(pixel_activation.super_pix)) << 2;

const column = dcol + (@as(u8, @intVast(pixel_activation.pix)) / @as(u8, 4));
const row = super_pix + (pixel_activation.pix & 0x3);

var toa: u32 = @intCast(pixel_activation.spidr_time);
toa <<= 14;
toa += @intCast(pixel_activation.toa_coarse);
toa <<= 4;
toa -= @intCast(pixel_activation.toa_fine);

The Magic values for a few other packets are known. However these packets have rarely proven useful in my work.

// When used when in counted read out mode, like a normal camera.
const CountedPixel = packed struct(u64) {
   _unknown: u60, // Actually not unknown, I just never needed it :shrug:
   magic: u4,     // Must be 0xa
};

// Carries signals and metadata from the SPIDR ASIC.
// Note: The open/close shutter events coincide with the start/stop of a
//       measurement in the control software.
const SPIDR = packed struct(u64) {
    _reserved_a: u12
    timestamp: u32,   // Counts 25ns.
    _reserved_b: u10,
    signal: u4,       // 0xF open shutter, 0xA close shuttper, 0xC "heatbeat" (purpose unknown)
    magic: u4,        // Must be 0x5
};

// Special case of the SPIDR packet with different layout.
const SPIDRPacketCount = packed struct(u64) {
    packet_count: u48, // Name known, purpose unknown.
    _reserved: u8,
    magic: u8, // Must be 0x50
};

// Carries signals and metadata from the TPX3 sensor chip.
// Note: While the names of these signals are known, their purpose is not.
const TPX3 = packed struct(u64) {
    _unknown_or_reserved: u48, // Currently not clear if unused/reserved or simply unknown.
    signal: u4, // 0x0 end of readout, 0xF start of readout, 0x4 and 0x5 undocumented
    mode: u4, // 0xA sequential, 0xB data driven, 0x4 undocumented but witnessed in real data
    magic: u8,
};

// Regular global time heart beat. Typically quite useless: If your
// measurement is long enough to trigger time overflow in the pixel
// activation events, you need to keep track of time rollover anyway,
// rendering the point of this packet quite moot.
//
// Major and minor global time are always send together.
const GlobalTimeMajor = packed struct(u64) {
    spidr_time: u16, // Counts 0.4096ms
    timestamp: u16,  // Counts 107.374182s
    _reserved: 24,
    magic: u8, // Must be 0x45
};

const GlobalTimeMinor = packed struct(u64) {
    spidr_time: u16, // Counts 0.4096ms
    timestamp: u32,  // Counts 25ns
    _reserved: u8,
    magic: u8, // Must be 0x44
};

// TDC refers to the additional event logger functionality often found
// SPIDR based timepix3 sensors. In response to an external electrical
// signal, an event is triggered.
const TDC = packed struct(u64){
    _reserved: u5,
    fine_timestamp: u35, // Counts 3.125ns
    trigger_counter: u12, // Purpose unknown.
    channel: u4, // 0xF CH1 rise, 0xA CH1 fall, 0xE CH2 rise, 0xB CH2 fall
    magic: u4, // Must be 0x6
};

Detecting ToA Rollovers

Pixel activation events have a maximum ToA of 26.8435456s after which this timestamp silently rolls over. You need to be careful when trying to detect this. Pixel activation events in the data stream are not strictly monotonic in time (only roughly). Naive rollover detectors may be accidentally triggered multiple times in succession when pixel actication from before and after a rollover arrive interleaved.

A good method for detecting rollovers is to separate the possible ToA time interval into two regimes: Start and end.

When in start, assume a rollover has happened recently. ToA over twenty seconds are assumed to be from before the rollover, below from after. If we see ToA over fifteen seconds, but below twenty seconds, assume we are nearing a rollover and enter the end regime.

When in end, assume ToA over five seconds are from the current interval. ToA below five second mean a rollover has happened: Increase the rollover counter and enter the start regime.

If you use a 53bit integer to store the pixel activation ToA (counting 1.5625ns), you can represent the maximum possible timestamp of 81 days, 10 hours and 41 minutes, after which the global time rolls over.

TDC events are strictly monotonic in ToA and can function with a naive rollover detector that simply checks if the new ToA is lower than the most recently seen one.

Clustering

Incident particles typically trigger multiple pixels on a timepix3 based sensor. For example, electrons will cause short "snakes" whereas photons will cause circles. If you desire to track individual particles, you likely want to cluster your data.

Adding the ToA as a third axis, pixel activations can be clustered in three dimensions, using any of the typical algorithms. This however is rather slow. For faster clustering at only marginally reduced accuracy, a new algorithm was proposed in 2024↗ that exploits the rough temporal ordering of pixel activation events. Not explicitly pointed out in the paper, this algorithm creates tiny leftover / "orphan" clusters from events that were not sorted into the correct cluster and as such formed their own. These can be thresholded out to better the signal to noise ratio.

If you are interested in a highly efficient implementation of this algorithm, feel free to contact me.

To detect the actual site of incident of the particle that triggered the cluster, multiple methods are available. Fast ones include highest/lowest ToA/ToT. Which of those you use depends on your use case, although I personally would advise against the ToA based ones. The pixels of Timepix3 sensors each have a small time offset in the ToA channel from the mean. Unless you have a good map of those offsets you can subtract from your data, ToA based incident site detection will likely suffer from low accuracy.